<template>
	<el-dialog title="选择人员" :visible.sync="visible" append-to-body width="992px">
		<div class="sw_select_user" v-loading="loading">
			<div class="query_user_wrapper" v-loading="tableLoading">
				<el-form size="mini" inline>
					<el-form-item><el-input v-model="queryData.account" placeholder="用户账号" style="width: 180px"></el-input></el-form-item>
					<el-form-item><el-input v-model="queryData.userName" placeholder="用户姓名" style="width: 180px"></el-input></el-form-item>
					<el-form-item>
						<el-select v-model="queryData.type" clearable placeholder="用户类型" style="width: 100px">
							<el-option v-for="(item, index) in dictEnums['USER_TYPE']" :key="index" :label="item.msg" :value="item.value"></el-option>
						</el-select>
					</el-form-item>
					<el-form-item>
						<el-select v-model="queryData.sex" clearable placeholder="用户性别" style="width: 100px">
							<el-option v-for="(item, index) in dictEnums['USER_SEX']" :key="index" :label="item.msg" :value="item.value"></el-option>
						</el-select>
					</el-form-item>
					<el-form-item><el-button type="primary" @click="queryTableData">查询</el-button></el-form-item>
				</el-form>
				<el-table :data="tableData" v-bind="tableConfig" @select-all="handleSelectAllChange" @row-click="handleRowClick">
					<el-table-column type="selection" width="48" align="center" :selectable="returnSelectable"></el-table-column>
					<el-table-column prop="account" label="用户账号" width="160"></el-table-column>
					<el-table-column prop="userName" label="用户姓名" width="160"></el-table-column>
					<el-table-column prop="deptName" label="用户性别" width="80">
						<template slot-scope="{ row }">
							<dict-tag dict="USER_SEX" :data="row.sex"></dict-tag>
						</template>
					</el-table-column>
					<el-table-column prop="deptName" label="用户部门" min-width="200"></el-table-column>
					<el-table-column prop="type" label="用户类型" width="80" align="center">
						<template slot-scope="{ row }">
							<dict-tag dict="USER_TYPE" :data="row.type"></dict-tag>
						</template>
					</el-table-column>
				</el-table>
				<sw-pagination :layout="userPageLayout" :total="dataCount" :page.sync="queryData.page" :limit.sync="queryData.limit" @change="queryTableData"></sw-pagination>
			</div>
			<div class="select_user_wrapper">
				<div class="list">
					<div class="item" v-for="(item, index) in checkedUser" :key="index">
						<el-avatar size="medium" shape="square" :src="item.picAttr"></el-avatar>
						<h4>{{ item.relName }}</h4>
						<el-button size="medium" type="text" icon="el-icon-delete" v-if="userInfo.id !== item.relId" @click="handleDeleteUser(item)"></el-button>
					</div>
				</div>
				<div class="button">
					<el-button size="small" @click="visible = false">取消</el-button>
					<el-button size="small" type="primary" @click="handleSubmitUser()">确定</el-button>
				</div>
			</div>
		</div>
	</el-dialog>
</template>

<script>
import lodash from 'lodash';
import { mapGetters } from 'vuex';
import { getUserList } from '@/api/system/tbSysUser';
export default {
	props: {
		value: { type: Boolean, default: false },
		data: { type: Array, default: () => [] }
	},
	data() {
		return {
			loading: false,
			visible: this.value,
			dataCount: 0,
			queryData: { page: 1, limit: 10 },
			tableData: [],
			checkedUser: [],
			tableConfig: {
				ref: 'table',
				rowKey: 'id',
				height: '100%',
				highlightSelectionRow: true
			},
			tableLoading: false
		};
	},
	computed: {
		...mapGetters(['dictEnums', 'siteInfo', 'userInfo']),
		userPageLayout() {
			return 'slot, prev, pager, next';
		}
	},
	watch: {
		value(flag) {
			this.visible = flag;
		},
		async visible(flag) {
			this.$emit('input', flag);
			if (!flag) return false;
			this.queryData.page = 1;
			this.queryTableData();
			this.checkedUser = lodash.cloneDeep(this.data);
		},
		tableData(arr) {
			this.$nextTick(() => {
				arr.forEach((item) => {
					let isChecked = this.checkedUser.some((n) => n.relId === item.id);
					this.$refs['table'].toggleRowSelection(item, isChecked);
				});
			});
		}
	},
	methods: {
		//获取用户列表
		async getTabelData() {
			let params = { ...this.queryData };
			let result = await getUserList(params).then((res) => res.data);
			this.tableLoading = false;
			if (result.code === '0') {
				this.dataCount = parseInt(result.count);
				this.tableData = !result.data ? [] : result.data;
			}
		},

		//查询用户数据
		queryTableData() {
			this.tableLoading = true;
			this.getTabelData();
		},

		//返回复选框是否可以选中
		returnSelectable(row) {
			return row.id !== this.userInfo.id;
		},

		//获取头像图片
		getAvatarImage(data) {
			if (!!data.picId && data.picId !== '-1') {
				return window.staticPath + data.picPath;
			} else if (!!this.siteInfo.userPic) {
				return window.staticPath + this.siteInfo.userPicPath;
			} else {
				return require('@/assets/img/avatar_image.png');
			}
		},

		//点击行选中用户
		handleRowClick(row) {
			if (this.userInfo.id !== row.id) {
				let index = this.checkedUser.findIndex((n) => n.relId === row.id);
				if (index !== -1) {
					this.checkedUser.splice(index, 1);
				} else {
					let picAttr = this.getAvatarImage(row);
					this.checkedUser.push({ relId: row.id, relName: row.userName, picAttr });
				}
				this.$refs['table'].toggleRowSelection(row);
			}
		},

		handleSelectAllChange(selection) {
			if (selection.length > 0) {
				selection.forEach((row) => {
					if (!this.checkedUser.some((n) => n.relId === row.id)) {
						let picAttr = this.getAvatarImage(row);
						this.checkedUser.push({ relId: row.id, relName: row.userName, picAttr });
					}
				});
			} else {
				this.tableData.forEach((row) => {
					let index = this.checkedUser.findIndex((n) => n.relId === row.id);
					this.checkedUser.splice(index, 1);
				});
			}
		},

		//删除选中用户
		handleDeleteUser(data) {
			let index = this.checkedUser.findIndex((n) => n.relId === data.relId);
			let userData = this.tableData.find((n) => n.id === data.relId);
			this.$refs['table'].toggleRowSelection(userData, false);
			this.checkedUser.splice(index, 1);
		},

		//提交选中用户
		handleSubmitUser() {
			this.visible = false;
			this.$emit('finish', this.checkedUser);
		}
	}
};
</script>

<style lang="scss" scoped>
.sw_select_user {
	display: flex;
	height: calc(70vh - 72px);
	width: calc(100% + 30px);
	margin: -15px;
	overflow: hidden;
}

.query_user_wrapper::v-deep {
	display: flex;
	flex-direction: column;
	flex: 1;
	overflow: hidden;
	.el-form {
		padding: 10px;
		.el-input {
			display: flex;
		}
	}
	.el-form-item {
		margin-bottom: 0;
	}
	.el-form-item__content {
		display: flex;
		align-items: center;
	}
	.el-table {
		border-top: solid 1px #e5e5e5;
	}
	.el-table__row {
		cursor: pointer;
		&.selection-row {
			background-color: var(--primary-background-plain-disabled);
		}
	}
	.sw-pagination {
		padding: 12px;
		margin-top: 0;
	}
}

.select_user_wrapper {
	display: flex;
	flex-direction: column;
	width: 240px;
	flex-shrink: 0;
	border-left: solid 1px #e5e5e5;
	.list {
		flex: 1;
		padding: 4px 0;
		overflow: hidden;
		overflow-y: auto;
		border-bottom: solid 1px #e5e5e5;
	}
	.item {
		display: flex;
		align-items: center;
		padding: 6px 10px;
		.el-avatar {
			flex-shrink: 0;
		}
		h4 {
			flex: 1;
			font-size: 14px;
			margin-left: 10px;
			overflow: hidden;
		}
		.el-button {
			flex-shrink: 0;
		}
	}
	.button {
		padding: 10px;
		text-align: right;
	}
}
</style>
